<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 金额
 * @author www.dingwei.cn
 */
class Money extends MY_Controller {
	
	/**
	 * 银行模型
	 * @var ConsumerBankModel
	 */
	public $mBank;
    /**
     * 银行模块
     * @var AdmBankModel
     */
    public $mAdmBank;
	/**
	 * 银行模型
	 * @var ConsumerUserModel
	 */
	public $mUser;
	
	public function __construct(){
		parent::__construct();
        $this->loadModel('ConsumerBankModel', 'mBank', 'consumer');
	}

    /**
     * 账户余额
     */
	public function index(){
        $list = $this->mUser->appGetBuyLogList($this->userId,1,0,1);
        $info = $this->mUser->getInfo($this->userId);
        $data = array(
            'pay_pwd' => isset($info['pay_passwd']) ? true : false,
            'list' => $list,
            'title' => '余额'
        );
        $this->view('money/index', $data);
    }

    /**
     * 资金明细
     */
    public function balanceLog(){
        $list = $this->mUser->appGetBuyLogList($this->userId,0,0,1);
        $data = array(
            'list' => $list,
            'title' => '资金明细'
        );
        $this->view('money/balanceLog', $data);
    }

    /**
     * 获取余额记录
     */
    public function getBalanceLog(){
    	//表单验证
    	$vConf = array(
            array('page', '分页', 'required|is_natural_no_zero'),
    	);
    	$this->formDataValidation($vConf);
    	
    	$page = trim(post('page'));
    	$time = trim(post('time'));
    	$type = (int)post('type');
        $type = $type == 0 && isset($time) ? 2 : $time;
    	$list = $this->mUser->appGetBuyLogList($this->userId,$type,$time,$page);
    	$this->ajaxReturn(1,'', $list);
    }

    /**
     * 提现
     */
    public function withdrawals(){
        $cardId = (int)get('card_id');
        //获取银行卡
        $cardInfo = array();
        if ($cardId > 0) {
            $cardInfo = $this->mBank->getInfo($cardId, $this->userId);
            if (!$cardInfo) {
                show_error('银行卡不存在');
            }
        }
        $data = array(
            'cardInfo' => $cardInfo,
            'title' => '申请提现'
        );
        $this->view('money/withdrawals', $data, 'wx_validation,layer_mobile');
    }

    /**
     * 充值
     */
    public function recharge(){
        $data = array(
            'title' => '充值'
        );
        $this->view('money/recharge', $data);
    }

    ////////////////////////////////////////////////////////////
    //-----------------------------------------------
    //|	银行卡
    //-----------------------------------------------
    /**
     * 我的银行卡
     */
    public function bank(){
        $bank = $this->getBankList(1);
        $aliPay = $this->getBankList(2);

        $data = array(
            'type' => (int)get('type'), // 1 提现
            'aliPay' => $aliPay,
            'bank' => $bank,
            'title' => '银行卡'
        );
        $this->view('money/bank', $data, 'layer_mobile');
    }

    /**
     * 获取银行卡列表
     * @param $typeId       int         1 - 银行卡 2 - 三方
     * @return array|array[]|number[]
     */
    public function getBankList($typeId){
    	$tA = $this->mBank->appGetBankList($this->userId,$typeId);
    	foreach ($tA as $k=>$v){
    		$tA[$k]['account_number'] = $this->mBank->HidesNumber($v['account_number'],4,4);
    	}
    	return $tA;
    }
    
    /**
     * 增加银行卡
     */
    public function addBankCard(){
        if (!empty($_POST)) {
            //表单验证
            $vConf = array(
                array('bank_id', '银行卡id', 'required|is_natural_no_zero'),
                array('account_name', '卡名称', 'required'),
                array('account_number', '卡号', 'required'),
                array('mobile', '联系电话', 'required|valid_mobile'),
            );
            $this->formDataValidation($vConf);

            $bankId = trim(post('bank_id'));
            $account_name = trim(post('account_name'));
            $account_number = trim(post('account_number'));
            $mobile = trim(post('mobile'));

            //获取银行卡
            $tBank = $this->mBank->getAdminBank($bankId);
            if (!$tBank) {
                $this->ajaxReturn(0, '银行卡类型不存在');
            }

            $data = array(
                'consumer_id' => $this->userId,
                'type' => 1,
                'icon' => $tBank['bank_icon'],
                'account_name' => $account_name,
                'account_number' => $account_number,
                'open_bank' => $tBank['bank_name'],
                'mobile' => $mobile,
                'addtime' => time(),
            );

            $tId = $this->mBank->save($data);
            if (!$tId) {
                $this->ajaxReturn(0, '添加失败');
            }

            $this->ajaxReturn(1, '添加成功', null, getUrl('money/bank'));
        }else{
            $this->loadModel('AdmBankModel', 'mAdmBank', 'admin');
            $list = $this->mAdmBank->getList();
            $bankList = array();
            foreach ($list as $k => $v){
                $bankList[] = array(
                    'title' => $v['bank_name'],
                    'value' => $v['id'],
                );
            }
            unset($list);
            $data = array(
                'list' => json_encode($bankList, JSON_UNESCAPED_UNICODE),
                'title' => '添加银行卡'
            );
            $this->view('money/addBankCard', $data, 'wx_validation,layer_mobile');
        }
    }
    
    /**
     * 增加支付宝
     */
    public function addAliPay(){
        if (!empty($_POST)) {
            //表单验证
            $vConf = array(
                array('account_name', '卡名称', 'required'),
                array('account_number', '卡号', 'required'),
            );
            $this->formDataValidation($vConf);

            $account_name = trim(post('account_name'));
            $account_number = trim(post('account_number'));

            //获取银行卡

            $data = array(
                'consumer_id' => $this->userId,
                'type' => 2,
                'icon' => '',
                'account_name' => $account_name,
                'account_number' => $account_number,
                'open_bank' => '支付宝',
                'mobile' => $account_number,
                'addtime' => time(),
            );

            $tId = $this->mBank->save($data);
            if (!$tId) {
                $this->ajaxReturn(0, '添加失败');
            }

            $this->ajaxReturn(1, '添加成功', null, getUrl('money/bank'));
        }else{
            $data = array(
                'title' => '添加支付宝'
            );
            $this->view('money/addAliPay', $data, 'wx_validation,layer_mobile');
        }
    }
    
    /**
     * 移除银行卡
     */
    public function rmBankCard(){
    	//表单验证
    	$vConf = array(
    			array('card_id', 'id', 'required'),
    	);
    	$this->formDataValidation($vConf, array('card_id' => get('card_id')));
    	
    	$cardId = (int)get('card_id');
    	
    	//获取银行卡
    	
    	if ( ! $this->mBank->getInfo($cardId,$this->userId)){
    		$this->ajaxReturn(0,'银行卡不存在');
    	}
    	
    	//删除银行卡
    	$this->db->where('id',$cardId);
    	$this->db->where('consumer_id',$this->userId);
    	if ( ! $this->db->delete($this->mBank->tbBank)){
    		$this->ajaxReturn(0,'移除失败');
    	}
    	
    	$this->ajaxReturn(1,'移除成功', null, getUrl('money/bank'));
    }

}